/**
 * The ToolBar is where all the action buttons to modifiy items are to be placed
 * @author Remo Lemma and Patrick Zulian
 * @version 0.1
 */


package ch.unisi.inf.projects.java.uom.gui;

 

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.border.SoftBevelBorder;

import ch.unisi.inf.projects.java.uom.gui.UOMImages;
import ch.unisi.inf.projects.java.uom.gui.commands.IconedCommand;
import ch.unisi.inf.projects.java.uom.elements.*;
import ch.unisi.inf.projects.java.uom.*;
import ch.unisi.inf.projects.java.uom.gui.commands.*;
import ch.unisi.inf.projects.java.uom.gui.itemeditor.ItemEditorListener;

public final class ToolBar extends JPanel implements ItemEditorListener, ItemManagerListener
{

	private static final long serialVersionUID = -1L;
	private JLabel itemLabel;
	private final List<ToolBarListener> listeners;
	private final List<JButton> buttons;
	private final ItemCreateCommand createItemCommand;

	public ToolBar()
    { 
		this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
		buttons = new ArrayList<JButton>();
		listeners = new ArrayList<ToolBarListener>();
        final JButton createCatbutton = new JButton(new ItemClassGUICreateCommand());
        buttons.add(createCatbutton);
        final JButton deleteCatButton = new JButton(new ItemClassGUIDeleteCommand());
        buttons.add(deleteCatButton);
        this.add(Box.createHorizontalStrut(5));
        this.add(createCatbutton);
        this.add(Box.createHorizontalStrut(10));
        this.add(deleteCatButton);
        createItemCommand = new ItemCreateCommand(null,true);
        final JButton itemButton = new JButton(createItemCommand);
        buttons.add(itemButton);
        this.add(Box.createHorizontalStrut(10));
        this.add(itemButton);
        this.add(Box.createHorizontalGlue());
        final JButton saveButton = new JButton(new IconedCommand("", "Saves the current displayed item", UOMImages.SAVE.getFileName()) {
			private static final long serialVersionUID = -1L;
        	public void actionPerformed (final ActionEvent e) {
				fireSaveItemPushed();
		}});
        itemLabel = new JLabel();
        this.add(itemLabel);
        this.add(Box.createHorizontalGlue());
        this.add(saveButton);
        this.add(Box.createHorizontalStrut(5));
        buttons.add(saveButton);
        setStateToButtons (ItemManager.instance().databaseIsOpen() == true);
    	ItemManager.instance().addItemManagerListener(this);
    }

	public void itemChanged(final Item newItem) {
		if(newItem != null){
			String labelText = newItem.getName();
			if(labelText.contains(Constants.ITEM_NEW.value())) {
				String[] splittedText = labelText.split(Constants.ITEM_PARSECHAR.value());
				if(splittedText.length >= 3) 
				{
					labelText = splittedText[1] +" " + splittedText[0] + " Template, " + splittedText[0] + " *"; //+ splittedText[2] 
				}}
			else {
			String newItemName = newItem.getField("STRING_NAME");
			if (newItemName != null)
				labelText = newItemName;
			}
		
			itemLabel.setText(" " + labelText + " ");
			itemLabel.setForeground(new Color(90,90,190));
			itemLabel.setToolTipText(ItemHTMLConverter.convertItem(newItem, false));
			itemLabel.setBackground(Color.WHITE);
			itemLabel.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
			createItemCommand.setItemClass(newItem.getItemClass());
			this.revalidate();
			this.repaint();
		}
		else
			createItemCommand.setItemClass(null);
	}

	
	public void addItemRelatedCommand(final Command command)
	{
		this.remove(this.getComponent(this.getComponentCount() - 1));
		this.add(Box.createHorizontalStrut(10));
		this.add(new JButton(command));
		this.add(Box.createHorizontalStrut(5));
	}
	
	
	public void itemDeleted() {return;}
	public void itemSaved() {return;}

	
	//listeners management
	
	public void addUOMToolBarListener(final ToolBarListener li) {
		listeners.add(li);
	}

	
	private void fireSaveItemPushed()
	{
		for(final ToolBarListener li : listeners)
		{
			li.saveItemPushed();
		}
	}
	
	private void setStateToButtons (final boolean state)
	{
		for (final JButton button : buttons)
			button.setEnabled(state);
	}
	
	public void databaseClosed () { itemLabel.setText(""); setStateToButtons(false); itemLabel.setVisible(false); revalidate(); repaint(); }
	public void databaseOpened () { createItemCommand.setItemClass(null); setStateToButtons(true); itemLabel.setVisible(true); revalidate(); repaint(); }
	public void elementDeleted (final Element e) { itemLabel.setText(""); }
	public void elementModified (final Element e) { return; }
	public void focusOnItem (final Item i) { return; }
	public void itemAdded (final Item i) { return; }
	public void itemClassAdded (final ItemClass itemClass) { return; }
	
}